package com.hundsun.yr.universal.p2p.ui.activity;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.utils.MathUtils;
import com.hundsun.yr.universal.library.view.hsdialog.HSDialog;
import com.hundsun.yr.universal.library.view.hsdialog.ZDialog;
import com.hundsun.yr.universal.library.view.itemview.BasicItemViewH;
import com.hundsun.yr.universal.library.view.itemview.SettingView;
import com.hundsun.yr.universal.library.view.itemview.SettingViewItemData;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.p2p.bean.AdSlideBeen;
import com.hundsun.yr.universal.p2p.bean.MixrepayPlanBean;
import com.hundsun.yr.universal.p2p.bean.MyLoanListBeen;
import com.hundsun.yr.universal.p2p.bean.PrepayPlanBeen;
import com.hundsun.yr.universal.p2p.bean.RepayPlanBeen;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.interactor.impl.MineLoanDetailInteractorImpl;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.ItemViewPresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.ui.view.LoanDateItem;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.utils.Logs;
import com.hundsun.yr.universal.p2p.view.base.BaseItemView;
import com.hundsun.yr.universal.p2p.view.base.InitializeView;

import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 还款详情页面
 *
 * @version V1.0
 * @Title: LoanDetailActivity.java
 * @Description: 借款详情页--申请中--待还款--已还款
 * @date 2015-12-3 上午9:57:00
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class LoanDetailActivity extends BaseActivity implements InitializeView, OnClickListener,
        HSDialog.HSDialogOnItemClickListener, BaseItemView {
    private ZDialog dialog, dialog1;
    private RequestPresenter mPresenter = null;
    /**
     * 还款button
     **/
    private HSButton btn_revoke = null;
    /**
     * 密集进度
     **/
    private ProgressBar progress_raise = null;
    /**
     * 状态父布局
     **/
    private LinearLayout linear_state = null;
    /**
     * 募集金额
     **/
    private TextView tv_raisemoney = null;
    /**
     * 状态
     **/
    private TextView tv_state = null;
    /**
     * 募集总额
     **/
    private TextView tv_raisetotle = null;
    /**
     * 受理单号
     **/
    private BasicItemViewH bv_acceptno = null;
    /**
     * 借款期限
     **/
    private TextView tv_loandate = null;
    /**
     * 借款利率
     **/
    private TextView tv_borrowingrate = null;
    /**
     * 手续费
     **/
    private TextView tv_counterfee = null;
    /**
     * 商品名称
     **/
    private TextView tv_goodsname = null;
    /**
     * 还款方式
     **/
    private TextView tv_refundway = null;
    /*    *//**
     * 弹窗
     **//*
    private HSDialog dialog = null;*/
    /**
     * 状态
     **/
    private String state = "";
    /**
     * 商品ID
     **/
    private String goodsId;
    /**
     * 借款详情对象
     **/
    private HashMap<String, String> map = null;
    /**
     * setttingview
     **/
    private SettingView setting_view = null;
    /**
     * 上个页面传递过来的数据
     **/
    private MyLoanListBeen myLoanListBeen;

    /**
     * 撤销申请
     */
    private HSButton btn_cancel_apply = null;

    /**
     * zwgt时间
     */
    private String zwgt_data;

    /**
     * 最下面一行小字
     */
//    private TextView tv_prepayment_trem;

    /**
     * 申请日期
     */
    private LoanDateItem ldt_apply_data;

    /**
     * 发布日期
     */
    private LoanDateItem ldt_release_data;

    /**
     * 起息日期
     */
    private LoanDateItem ldt_word_data;

    /**
     * 还清日期
     */
    private LoanDateItem ldt_pay_off_data;

    /**
     * 还款dialog
     */
//    private HSDialog dialog_repayment;

    /**
     * 取消dialog
     */
    private HSDialog cancel_dialog;

    /**
     * 提前还款dialog
     */
    private HSDialog dialog_prepayment;

    private HSDialog overPayDialog;
    /**
     * 账户余额不足
     */
    private HSDialog lackDialog;
    private ArrayList<RepayPlanBeen> repayPlanBeens;
    private int i;

    private int repaytimes = 0;
    private String prepayPlanId;
    private String prepaymentBeginTime;
    private String prepaymentEndTime;
    private String currrentTime;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void getBundleExtras(Bundle extras) {
        myLoanListBeen = (MyLoanListBeen) extras.getSerializable(Extras.DATA);
        goodsId = myLoanListBeen.uuid;
    }

    @Override
    protected int getContentViewLayoutID() {
        // TODO Auto-generated method stub
        return R.layout.act_loan_detail;
    }

    @Override
    protected void onEveComming(@SuppressWarnings("rawtypes") EventCenter eventCenter) {
    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void initViewsAndEvents() {
        btn_cancel_apply = findView(R.id.btn_cancle_apply);
        btn_revoke = findView(R.id.btn_revoke);
//        if (!TextUtils.equals(myLoanListBeen.pay_method, "1") && myLoanListBeen.repayed_term != null && !myLoanListBeen.repayed_term.isEmpty()) {
//            btn_revoke.setText(getString(R.string._repayment, MathUtils.plus(myLoanListBeen.repayed_term, "1")));
//        }

        setToolbarTitle(R.string.loan_detail);

        linear_state = findView(R.id.linear_state);

        tv_raisemoney = findView(R.id.tv_raisemoney);
        tv_state = findView(R.id.tv_state);
        tv_loandate = findView(R.id.tv_loandate);
        tv_borrowingrate = findView(R.id.tv_borrowingrate);
        tv_counterfee = findView(R.id.tv_counterfee);
        tv_raisetotle = findView(R.id.tv_raisetotle);
        bv_acceptno = findView(R.id.bv_acceptno);
        tv_refundway = findView(R.id.tv_refundway);
        progress_raise = findView(R.id.progress_raise);
        setting_view = findView(R.id.setting_view);

//        tv_prepayment_trem = findView(R.id.tv_prepayment_trem);

        ldt_apply_data = findView(R.id.ldt_apply_data);
        ldt_release_data = findView(R.id.ldt_release_data);
        ldt_word_data = findView(R.id.ldt_word_data);
        ldt_pay_off_data = findView(R.id.ldt_pay_off_data);
        mPresenter = new ItemViewPresenterImpl(mContext, this, new MineLoanDetailInteractorImpl());
        mPresenter.initialized();

        //显示数据
        refreshUI(myLoanListBeen);

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    /**
     * 刷新数据
     */
    private void refreshUI(MyLoanListBeen myLoanListBeen) {
        /**已募集金额**/
        tv_raisemoney.setText(getString(R.string._yuan, myLoanListBeen.true_debt == null ? "0" : myLoanListBeen.true_debt));

        /**商品名称--还款状态**/
        if (!TextUtils.equals(myLoanListBeen.applyprogress, "10")) {
            if (myLoanListBeen.prdname != null && !myLoanListBeen.prdname.isEmpty()) {
                bv_acceptno.getmSubTitle().setText(myLoanListBeen.getApplyProgress());
                bv_acceptno.getmTitle().setText(myLoanListBeen.prdname);
            } else {
                bv_acceptno.getmSubTitle().setText(myLoanListBeen.getApplyProgress());
                bv_acceptno.getmTitle().setText("申请编号" + myLoanListBeen.uuid);
            }
        } else {
            if (myLoanListBeen.prdname != null && !myLoanListBeen.prdname.isEmpty()) {
                bv_acceptno.getmSubTitle().setText(myLoanListBeen.getPrdprogress());
                bv_acceptno.getmTitle().setText(myLoanListBeen.prdname);
            } else {
                bv_acceptno.getmSubTitle().setText(myLoanListBeen.getPrdprogress());
                bv_acceptno.getmTitle().setText("申请编号" + myLoanListBeen.uuid);
            }
        }


        /**申请总额**/
        tv_raisetotle.setText(getString(R.string._raise_totle, myLoanListBeen.amount));

        /**借款期限**/
        tv_loandate.setText(myLoanListBeen.getTerm());

        /**手续费**/
        tv_counterfee.setText(myLoanListBeen.total_interest);

        /**借款利率**/
        if (!myLoanListBeen.debt_rate.isEmpty()) {
            tv_borrowingrate.setText(CommTool.keep1Digits(new BigDecimal(myLoanListBeen.debt_rate)) + "%");
        } else {
            tv_borrowingrate.setText(CommTool.keep1Digits(new BigDecimal("0")) + "%");
        }

        /**还款方式**/
        tv_refundway.setText(myLoanListBeen.getPayMethod(mContext));

        /**募集进度的显示**/
        if (myLoanListBeen.true_debt != null) {
            progress_raise.setProgress((int) Math.floor(MathUtils.string2float(String.valueOf(Double.valueOf(myLoanListBeen.true_debt) / Double.valueOf(myLoanListBeen.amount))) * 100));
        } else {
            progress_raise.setProgress((int) Math.floor(MathUtils.string2float(String.valueOf(0 / Double.valueOf(myLoanListBeen.amount))) * 100));
        }

        ldt_apply_data.setSubTitle(myLoanListBeen.apply_day);
        ldt_release_data.setSubTitle(myLoanListBeen.onsale_day);
        ldt_word_data.setSubTitle(myLoanListBeen.start_day);

        if (TextUtils.equals(myLoanListBeen.prdprogress, "50") || TextUtils.equals(myLoanListBeen.prdprogress, "40")) {
            if (myLoanListBeen.repay_time != null && !myLoanListBeen.repay_time.isEmpty()) {
                ldt_pay_off_data.setSubTitle(myLoanListBeen.repay_time);
            } else {
                ldt_pay_off_data.setSubTitle(myLoanListBeen.end_day);
            }
        }

        /**
         * 先根据申请状态，定义ui
         0	用户草稿
         1	一审（风控）审批中
         2	二审（运营）审批中
         10	审批完成，已转产品。
         100	用户撤回
         110	一审（风控）拒绝
         120	二审（运营）拒绝
         */
        if (TextUtils.equals(myLoanListBeen.applyprogress, "0") || TextUtils.equals(myLoanListBeen.applyprogress, "1")) {
            //有撤销按钮
            btn_cancel_apply.setVisibility(View.VISIBLE);
            //起息日期
            ldt_word_data.setVisibility(View.GONE);
            //还清日期
            ldt_pay_off_data.setVisibility(View.GONE);
            btn_revoke.setVisibility(View.GONE);
//            tv_prepayment_trem.setVisibility(View.GONE);
            setting_view.setVisibility(View.GONE);
            btn_cancel_apply.setOnClickListener(this);
        } else if (TextUtils.equals(myLoanListBeen.applyprogress, "2")) {
            btn_cancel_apply.setVisibility(View.GONE);
            //起息日期
            ldt_word_data.setVisibility(View.GONE);
            //还清日期
            ldt_pay_off_data.setVisibility(View.GONE);
            btn_revoke.setVisibility(View.GONE);
//            tv_prepayment_trem.setVisibility(View.GONE);
            setting_view.setVisibility(View.GONE);
        } else if (TextUtils.equals(myLoanListBeen.applyprogress, "10")) {
            //产品已经发布，根据产品状态，做显示
            if (TextUtils.equals(myLoanListBeen.prdprogress, "20")) {
                if (!TextUtils.equals(myLoanListBeen.pay_method, "1")) {
                    //申请进度已转产品
                    toolbar_menu.setText(R.string.repay_plan);
                    toolbar_menu.setTextColor(getResources().getColor(R.color.black));
                    toolbar_menu.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    toolbar_menu.setTypeface(Typeface.DEFAULT);
                    toolbar_menu.setOnClickListener(this);
                }

                //待还款
                //申请撤销
                btn_cancel_apply.setVisibility(View.GONE);

                //paymethod为1的显示还款按钮，然后还有一堆条件智障条件
                prepaymentBeginTime = CommTool.date2time(myLoanListBeen.prepayment_begin_date);
                prepaymentEndTime = CommTool.date2time(myLoanListBeen.prepayment_end_date);
                currrentTime = System.currentTimeMillis() + "";

                if (TextUtils.equals(myLoanListBeen.pay_method, "1")) {
                    //请求综合还款
                    if (TextUtils.equals(myLoanListBeen.canPay, "1")) {
                        btn_revoke.setVisibility(View.VISIBLE);
                    } else {
                        btn_revoke.setVisibility(View.GONE);
                        Log.e("Canpay",myLoanListBeen.toString());
                    }
                } else if (!TextUtils.equals(myLoanListBeen.pay_method, "1")) {
                    //点击进入还款计划，进行逐一还款或者提前还款
                    if (TextUtils.equals(myLoanListBeen.canPay, "1")) {
                        btn_revoke.setVisibility(View.VISIBLE);
                    } else {
                        btn_revoke.setVisibility(View.GONE);
                    }
                } else {
                    btn_revoke.setVisibility(View.GONE);
                }

                //最下面一行小字
//                tv_prepayment_trem.setVisibility(View.VISIBLE);
                setting_view.setVisibility(View.VISIBLE);

            } else if (TextUtils.equals(myLoanListBeen.prdprogress, "30")) {
                if (!TextUtils.equals(myLoanListBeen.pay_method, "1")) {
                    //申请进度已转产品
                    toolbar_menu.setText(R.string.repay_plan);
                    toolbar_menu.setTextColor(getResources().getColor(R.color.black));
                    toolbar_menu.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    toolbar_menu.setTypeface(Typeface.DEFAULT);
                    toolbar_menu.setOnClickListener(this);
                }
                //待还款
                //申请撤销
                btn_cancel_apply.setVisibility(View.GONE);
                //还款按钮
                btn_revoke.setVisibility(View.GONE);

                setting_view.setVisibility(View.GONE);
            } else if (TextUtils.equals(myLoanListBeen.prdprogress, "50")) {
                if (!TextUtils.equals(myLoanListBeen.pay_method, "1")) {
                    //申请进度已转产品
                    toolbar_menu.setText(R.string.repay_plan);
                    toolbar_menu.setTextColor(getResources().getColor(R.color.black));
                    toolbar_menu.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    toolbar_menu.setTypeface(Typeface.DEFAULT);
                    toolbar_menu.setOnClickListener(this);
                }
                //待还款
                //申请撤销
                btn_cancel_apply.setVisibility(View.GONE);
                //还款按钮
                btn_revoke.setVisibility(View.GONE);

                //最下面一行小字
//                tv_prepayment_trem.setVisibility(View.VISIBLE);
                setting_view.setVisibility(View.GONE);
            } else if (TextUtils.equals(myLoanListBeen.prdprogress, "40")) {
                if (!TextUtils.equals(myLoanListBeen.pay_method, "1")) {
                    //申请进度已转产品
                    toolbar_menu.setText(R.string.repay_plan);
                    toolbar_menu.setTextColor(getResources().getColor(R.color.black));
                    toolbar_menu.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    toolbar_menu.setTypeface(Typeface.DEFAULT);
                    toolbar_menu.setOnClickListener(this);
                }
                //待还款
                //申请撤销
                btn_cancel_apply.setVisibility(View.GONE);
                //还款按钮
                btn_revoke.setVisibility(View.GONE);

                //最下面一行小字
//                tv_prepayment_trem.setVisibility(View.GONE);
                setting_view.setVisibility(View.GONE);
            } else {
                btn_cancel_apply.setVisibility(View.GONE);
                btn_revoke.setVisibility(View.GONE);
//                tv_prepayment_trem.setVisibility(View.GONE);
                setting_view.setVisibility(View.GONE);
            }

        } else if (TextUtils.equals(myLoanListBeen.applyprogress, "100")) {
            btn_cancel_apply.setVisibility(View.GONE);
            //起息日期
            ldt_word_data.setVisibility(View.GONE);
            //还清日期
            ldt_pay_off_data.setVisibility(View.GONE);
            btn_revoke.setVisibility(View.GONE);
//            tv_prepayment_trem.setVisibility(View.GONE);
            setting_view.setVisibility(View.GONE);
        } else if (TextUtils.equals(myLoanListBeen.applyprogress, "110")) {
            btn_cancel_apply.setVisibility(View.GONE);
            //起息日期
            ldt_word_data.setVisibility(View.GONE);
            //还清日期
            ldt_pay_off_data.setVisibility(View.GONE);
            btn_revoke.setVisibility(View.GONE);
//            tv_prepayment_trem.setVisibility(View.GONE);
            setting_view.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onNetworkConnected(NetType type) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onNetworkDisConnected() {
        // TODO Auto-generated method stub

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.btn_revoke://还款
                //查询还款计划
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                prepaymentBeginTime = CommTool.date2time(myLoanListBeen.prepayment_begin_date);
                prepaymentEndTime = CommTool.date2time(myLoanListBeen.prepayment_end_date);
                String next_repay_day = CommTool.date2time(myLoanListBeen.next_repay_day);
                currrentTime = System.currentTimeMillis() + "";
                if (TextUtils.equals(myLoanListBeen.pay_method, "1") && TextUtils.equals(myLoanListBeen.canPay, "1")) {
                    //到期还本付息 综合还款  先查询还款计划，在进行还款
                    Logs.e("TASK_MIXREPAYPLAN goodsId:77777 " + goodsId);
                    mPresenter.loadingData(TaskConstants.TASK_MIXREPAYPLAN, getString(R.string.root_url_test) + getString(R.string.api_user_test) + TaskConstants.TASK_MIXREPAYPLAN
                            + "?productid=" + goodsId, Constants.EVENT_PROGRESS, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_GET, getString(R.string.api_user_test) +
                            TaskConstants.TASK_MIXREPAYPLAN + "?productid=" + goodsId, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), false);
//                    if (MathUtils.compare(currrentTime, next_repay_day) <= 0){
//                        dialog_prepayment = new HSDialog("还未到还款日", "确认提前还款？",
//                                getString(R.string.cancel), getString(R.string.sure), mContext,
//                                LoanDetailActivity.this).setCancelable(false);//.setOnDismissListener(this);
//                    } else {
//                        dialog_prepayment = new HSDialog("提示", "确认还款",
//                                getString(R.string.cancel), getString(R.string.sure), mContext,
//                                LoanDetailActivity.this).setCancelable(false);//.setOnDismissListener(this);
//                    }
//
//                    dialog_prepayment.show();
//                    dialog_prepayment.setOnItemClickListener(LoanDetailActivity.this);
                } else if (!TextUtils.equals(myLoanListBeen.pay_method, "1") && TextUtils.equals(myLoanListBeen.canPay, "1")) {
                    //其他三种还款方式 点击之后进入还款计划
                    Bundle bundle = new Bundle();
                    bundle.putString("prepayment_flag", myLoanListBeen.prepayment_flag);
                    bundle.putString("goodsid", goodsId);
                    bundle.putString("prepayment_begin_date", myLoanListBeen.prepayment_begin_date);
                    bundle.putString("prepayment_end_date", myLoanListBeen.prepayment_end_date);
                    bundle.putString("repayment", myLoanListBeen.repayment);
                    bundle.putString("prdprogress", myLoanListBeen.prdprogress);
                    readyGo(RepayPlanActivity.class, bundle);
                }
                break;
            case R.id.toolbar_menu://还款计划
                Bundle bundle = new Bundle();
                bundle.putString("prepayment_flag", myLoanListBeen.prepayment_flag);
                bundle.putString("goodsid", goodsId);
                bundle.putString("prepayment_begin_date", myLoanListBeen.prepayment_begin_date);
                bundle.putString("prepayment_end_date", myLoanListBeen.prepayment_end_date);
                bundle.putString("repayment", myLoanListBeen.repayment);
                bundle.putString("prdprogress", myLoanListBeen.prdprogress);
                readyGo(RepayPlanActivity.class, bundle);
                break;
            case R.id.btn_cancle_apply://取消按钮
                cancel_dialog = new HSDialog(mContext.getString(R.string.tips), mContext.getString(R.string.tips_revoke_apply), mContext.getString(R.string.cancel), mContext.getString(R.string.sure), mContext, this).setCancelable(false);//.setOnDismissListener(this);
                cancel_dialog.show();
                break;
        }
    }

    @SuppressWarnings("rawtypes")
    @Override
    public <T> void initializeViews(String task_name, T data) {
        switch (task_name) {
            case TaskConstants.TASK_RETURN_MONEY:
                AdSlideBeen been = (AdSlideBeen) data;
                dialog = new ZDialog(this, "暂不签约", been.getMsg(), "立即签约", R.style.MyDialog);
                dialog.show();
                dialog.setOnclickListener(new ZDialog.OnclickListener() {
                    @Override
                    public void OnClickListener() {
                        readyGo(ManagementActivity.class);
                    }
                });

                break;
            case TaskConstants.TASK_CHARGE_CHARGE:
                AdSlideBeen adSlideBeen = (AdSlideBeen) data;
//                lackDialog = new HSDialog(mContext.getString(R.string.tips), adSlideBeen.getMsg(), "暂不充值",
//                        "立即充值", mContext, LoanDetailActivity.this).setCancelable(false);
//                lackDialog.show();
                dialog1 = new ZDialog(this, "暂不充值", adSlideBeen.getMsg(), "立即充值", R.style.MyDialog);
                dialog1.show();
                dialog1.setOnclickListener(new ZDialog.OnclickListener() {
                    @Override
                    public void OnClickListener() {
                        readyGo(RechangeNewActivity.class);
                    }
                });
                break;
//            case TaskConstants.TASK_REPAYPLAN:
//                if (data != null && !data.toString().isEmpty()) {
//                    //只有还款计划请求成功之后才能弹出dialog
//                    repayPlanBeens = (ArrayList<RepayPlanBeen>) data;
//                    //本期可以还款的条件，上一期为状态6已兑付
//
//                    if (i >= repayPlanBeens.size()) {//当前无还款期数
//                        Logs.e("当前无可还款期数i: " + i);
//                        return;
//                    }
//
//                    int finalI = i + 1;
//                    dialog_repayment = new HSDialog("当前还款:第" + finalI + "期", "还款金额" + repayPlanBeens.get(i).amount + "元",
//                            getString(R.string.cancel), getString(R.string.sure), mContext,
//                            this).setCancelable(false);//.setOnDismissListener(this);
//                    dialog_repayment.show();
//                    dialog_repayment.setOnItemClickListener(this);
//                }
//                break;
//
//            case TaskConstants.TASK_REPAY:
//                //还款成功之后暂时将还款按钮隐藏
////                btn_revoke.setVisibility(View.GONE);
//                showToast(getString(R.string.mine_ready_success));
//                finish();
//                // TODO: 2017/6/24
//                if (BaseAppManager.getInstance().getActivity(MineLoanActivity.class) != null) {
//                    BaseAppManager.getInstance().getActivity(MineLoanActivity.class).finish();
//                }
//
//                break;
            case TaskConstants.TASK_APPLYCANCEL://借款撤销
                showToast(getString(R.string.loan_revoke_success));
                finish();
                EventBus.getDefault().post(new EventCenter(Extras.LOAN_ORDER_SUCCESS));
                break;
            case TaskConstants.TASK_PREPAYPLAN://提前还款计划
//                flag = prepay;
                if (data != null) {
                    PrepayPlanBeen prepayPlanBeen = (PrepayPlanBeen) data;
                    String actual_amount = prepayPlanBeen.actual_amount;
                    prepayPlanId = prepayPlanBeen.id;

                    dialog_prepayment = new HSDialog("确定还款", "还款总金额" + actual_amount + "元",
                            getString(R.string.cancel), getString(R.string.sure), mContext,
                            LoanDetailActivity.this).setCancelable(false);//.setOnDismissListener(this);
                    dialog_prepayment.show();
                    dialog_prepayment.setOnItemClickListener(LoanDetailActivity.this);
                }
                break;

            case TaskConstants.TASK_MIXREPAYPLAN://mixrepayplan
//                flag = prepay;
                if (data != null) {
                    MixrepayPlanBean prepayPlanBeen = (MixrepayPlanBean) data;
                    String actual_amount = prepayPlanBeen.actualAmount;
//                    prepayPlanId = prepayPlanBeen.repayLogic;

                    dialog_prepayment = new HSDialog("确定还款", "还款总金额" + actual_amount + "元",
                            getString(R.string.cancel), getString(R.string.sure), mContext,
                            LoanDetailActivity.this).setCancelable(false);//.setOnDismissListener(this);
                    dialog_prepayment.show();
                    dialog_prepayment.setOnItemClickListener(LoanDetailActivity.this);
                }
                break;
/*            case TaskConstants.TASK_PREPAY://提前还款
                //提前还款成功
                //刷新一个借款页面
                finish();

                if (BaseAppManager.getInstance().getActivity(MineLoanActivity.class) != null) {
                    BaseAppManager.getInstance().getActivity(MineLoanActivity.class).finish();
                }

                showToast(getString(R.string.mine_already_success));

                break;*/

            case TaskConstants.TASK_NEWREPAY://综合还款接口
                finish();

                if (BaseAppManager.getInstance().getActivity(MineLoanActivity.class) != null) {
                    BaseAppManager.getInstance().getActivity(MineLoanActivity.class).finish();
                }

                showToast(getString(R.string.mine_ready_success));
                break;
        }
    }

    @Override
    public <T> void initializeError(String task_name, T data) {
        // TODO Auto-generated method stub
        showToast(data.toString());
    }

    @Override
    public void onItemClick(Object o, int position) {
        if ((HSDialog) o == cancel_dialog) {
            if (position != -1) {
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map = new JSONObject();
                try {
                    map.put("uuid", goodsId);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_APPLYCANCEL, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_loan_test)
                        + TaskConstants.TASK_APPLYCANCEL, Constants.EVENT_DATA, jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_loan_test)
                        + TaskConstants.TASK_APPLYCANCEL, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);
            }
            //未签还款授权
            //超出缴费授权金额
            //未签缴费授权
            //超出还款授权金额
        }/* else if ((HSDialog) o == dialog_repayment) {//普通还款
            if (position != -1) {
                //确认
                //还款--还款方式
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map = new JSONObject();

                if (repayPlanBeens == null) {
                    showToast(getString(R.string.network_anomaly));
                    dialog_repayment.dismiss();
                    return;
                }

                try {
                    map.put("goodsid", goodsId);
                    if (i >= repayPlanBeens.size()) {//防止角标越界
                        return;
                    }

                    map.put("planid", repayPlanBeens.get(i).id);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_REPAY, getString(R.string.root_url_test) + getString(R.string.api_trade_test) + TaskConstants.
                        TASK_REPAY, Constants.EVENT_DATA, jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, getString(R.string.api_trade_test) + TaskConstants
                        .TASK_REPAY, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);

            }
        } */ else if ((HSDialog) o == dialog_prepayment) {//综合还款
            if (position != -1) {
                //到期还本付息产品弹出还款
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map = new JSONObject();

                Logs.e("TASK_NEWREPAYgoodsid:77777 " + goodsId);

                try {
                    map.put("goodsid", goodsId);
                    map.put("planid", "");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                //综合还款
                mPresenter.loadingData(TaskConstants.TASK_NEWREPAY, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_trade_test)
                        + TaskConstants.TASK_NEWREPAY, Constants.EVENT_DATA, jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_trade_test)
                        + TaskConstants.TASK_NEWREPAY, sharedPreferences.getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data), true);

            }
        }

        //账户余额不足
        else if (o == lackDialog && position == -1) {
            readyGo(RechangeNewActivity.class);
        }
    }

    /***
     * 用来设置setting里面的东西
     *
     * @param data     * @param <T>
     */
    @Override
    public <T> void setAdapter(List<T> data) {
        setting_view.setAdapter((List<SettingViewItemData>) data);
        setting_view.modifySubTitle(myLoanListBeen.repayed_term + "/" + myLoanListBeen.retotal_term, 0);
        setting_view.modifySubTitle(myLoanListBeen.next_repay_day, 1);
        setting_view.modifySubTitle(getString(R.string.dialog_regiester_red_envelope, myLoanListBeen.next_repay_amt), 2);
    }
}
